﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DONGACS.Common;
using Webdiyer.WebControls.Mvc;
using QLPKWeb.Models.LinQ.Home;

using QLPKWeb.Models.FormBean.HomeMoDel;

namespace QLPKWeb.BussinessDelegate.HomeDelegate
{
    public class HomeDelegate
    {
        HomeForm HF =  new HomeForm();
        HomeDataContext DB= new HomeDataContext();
        public  PagedList<Task_TinTuc>  GetALL(int?id)
        {
            return DB.Task_TinTucs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public Task_TinTuc GetByID(int id)
        {
            return DB.Task_TinTucs.Single(p => p.Id == id);
        }
        public void InsertTinTuc(Task_TinTuc tinTuc)
        {
            DB.Task_TinTucs.InsertOnSubmit(tinTuc);
            DB.SubmitChanges();
        }
        public void UpdateTinTuc(Task_TinTuc tinTuc)
        {
            Task_TinTuc taskTinTuc = DB.Task_TinTucs.Single(p => p.Id == tinTuc.Id);
            taskTinTuc.TieuDe = tinTuc.TieuDe;
            taskTinTuc.NoiDungTomTac = tinTuc.NoiDungTomTac;
            taskTinTuc.NoiDungChiTiet = tinTuc.NoiDungChiTiet;
            taskTinTuc.MaNguon = tinTuc.MaNguon;
            taskTinTuc.NgayDang = tinTuc.NgayDang;
            DB.SubmitChanges();
        }
        public void UpdateTrangThai(Task_TinTuc tinTuc)
        {
            Task_TinTuc taskTinTuc = DB.Task_TinTucs.Single(p => p.Id == tinTuc.Id);
            taskTinTuc.Status = tinTuc.Status;
            DB.SubmitChanges();
        }
        public void DeleteTinTuc(Task_TinTuc tinTuc)
        {
            Task_TinTuc taskTinTuc = DB.Task_TinTucs.Single(p => p.Id == tinTuc.Id);
            DB.Task_TinTucs.DeleteOnSubmit(taskTinTuc);
            DB.SubmitChanges();
        }
        public SelectList builTieuChi(int? selectedValue)
        {
            IList<object> selectList = new List<object>();
            selectList.Add(new { value = 0, Text = "Tiêu đề" });
            selectList.Add(new { value = 1, Text = "Ngày đăng" });
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        } 

        public IList<Task_TinTuc> LayTop3()
        {
            return DB.Task_TinTucs.Where(t=>t.Status==true).Take(1).OrderByDescending(p =>p.Id).ToList();
        }
        public PagedList<Task_TinTuc> GetAllTinTuc(int ?id)
        {
            return DB.Task_TinTucs.Where(t => t.Status == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1,5);
        }

        public IList<dm_CoQuanSuDung> GetCQSD()
        {
            return DB.dm_CoQuanSuDungs.ToList();
        }
        public dm_CoQuanSuDung GetCQSDID(int id)
        {
            return DB.dm_CoQuanSuDungs.Single(p=>p.Id==id);
        }

        public PagedList<Task_TinTuc> Timkiem(int? id, string tungay, string denngay, string TieuChi, int Values)
        {
            if (Values == 0)
            {
                if (TieuChi == null || TieuChi == "")
                {
                    return DB.Task_TinTucs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
                }
                else
                {
                    return DB.Task_TinTucs.Where(p => p.TieuDe.Contains(TieuChi)).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
                }
            }
            else
            {
                if (tungay == "" || denngay == "")
                {
                    return DB.Task_TinTucs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
                }
                else
                {
                    return DB.Task_TinTucs.Where(p =>p.NgayDang >= DateTime.ParseExact(tungay, "dd/MM/yyyy", null) && p.NgayDang <= DateTime.ParseExact(denngay, "dd/MM/yyyy", null)).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
                }
            }
        }
        public IList<dm_NghiepVuCoQuan> GetAllIdCQ(int idCQ)
        {
            return DB.dm_NghiepVuCoQuans.Where(p => p.IdCoQuanSuDung == idCQ).ToList();
        }
    }
}